<?php

/**
 *    卖家的退货/收货地址控制器
 *
 *    @author    yanghj
 *    @date      2014.04.30
 */
class Seller_addressApp extends StoreadminbaseApp
{
    function index()
    {
        $this->returns();
     
    }
    
    /**
     *    收货地址
     *
     */   
    function consignee()
    {
        /* 取得列表数据 */
        $addr_id = empty($_GET['addr_id']) ? 0 : intval($_GET['addr_id']);
        $model_selleraddress =& m('selleraddress');
        $addresses     = $model_selleraddress->find(array(
            'conditions'    => 'user_id = ' . $this->visitor->get('user_id') . ' AND type = 0',
            'order'         => "addr_id DESC",
        ));
        $this->assign('addresses_list', $addresses);

        /* 当前位置 */
        $this->_curlocal(LANG::get('seller_center'),    'index.php?app=seller',
                         LANG::get('consignee_address'), 'index.php?app=seller_address&act=consignee',
                         LANG::get('address_list'));

        /* 当前用户中心菜单 */
        $this->_curitem('seller_address');

        /* 当前所处子菜单 */
        $this->_curmenu('address_list');  
        $this->import_resource(array(
                'script' => 'jquery.plugins/jquery.validate.js,jquery.plugins/additional-methods.js,mlselection.js',
            ));
        if($_GET['act_type']=='edit' && $addr_id !=0)
        {
            $find_data     = $model_selleraddress->find("addr_id = {$addr_id} AND user_id=" . $this->visitor->get('user_id'));
            if (empty($find_data))
            {
                echo Lang::get('no_such_address');

                return;
            }
            $address = current($find_data);
            if($address['phone_tel'])
             {
                $phone_tel = explode('-', $address['phone_tel']);
             }
             $this->assign('act', 'edit');
             $this->assign('phone_tel', $phone_tel);
             $this->assign('address', $address);
        }
        else
        {
             $this->assign('act', 'add');
        }            
        $this->_config_seo('title', Lang::get('seller_center') . ' - ' . Lang::get('consignee_address'));
        $this->_get_regions();
        $this->display('seller_address.consignee.html');
    }
    
    /**
     *    退货地址
     *
     */   
    function returns()
    {
        /* 取得列表数据 */
        $addr_id = empty($_GET['addr_id']) ? 0 : intval($_GET['addr_id']);
        $model_selleraddress =& m('selleraddress');
        $addresses     = $model_selleraddress->find(array(
            'conditions'    => 'user_id = ' . $this->visitor->get('user_id') . ' AND type = 1',
            'order'         => 'is_deafult DESC, addr_id DESC',
        ));
        $this->assign('addresses_list', $addresses);

        /* 当前位置 */
        $this->_curlocal(LANG::get('seller_center'),    'index.php?app=seller',
                         LANG::get('returns_address'), 'index.php?app=seller_address&act=returns',
                         LANG::get('address_list'));

        /* 当前用户中心菜单 */
        $this->_curitem('seller_address');

        /* 当前所处子菜单 */
        $this->_curmenu('address_list');
        $this->import_resource(array(
                'script' => 'jquery.plugins/jquery.validate.js,jquery.plugins/additional-methods.js,mlselection.js',
            ));
        if($_GET['act_type']=='edit' && $addr_id !=0)
        {
            $find_data     = $model_selleraddress->find("addr_id = {$addr_id} AND user_id=" . $this->visitor->get('user_id'));
            if (empty($find_data))
            {
                echo Lang::get('no_such_address');

                return;
            }
            $address = current($find_data);
            if($address['phone_tel'])
             {
                $phone_tel = explode('-', $address['phone_tel']);
             }
             $this->assign('act', 'edit');
             $this->assign('phone_tel', $phone_tel);
             $this->assign('address', $address);
        }
        else
        {
             $this->assign('act', 'add');
        }
        $this->_config_seo('title', Lang::get('seller_center') . ' - ' . Lang::get('returns_address'));
        $this->_get_regions();
        $this->display('seller_address.index.html');
    }

    /**
     *    添加地址
     *
     */
    function add()
    {
        
        if (!IS_POST)
        {
            $this->show_message('no_valid_data','back_list', 'index.php?app=seller_address&act=returns');
        }
        else
        {

            if (!$_POST['consignee'] || !$_POST['region_name'] || !$_POST['address'] || (!$_POST['phone_mob'] && !$_POST['phone_code']))
            {

               $this->show_warning('数据不完整!请重新填写!');
                return;
            }

            if ((!is_mobile($_POST['phone_mob']) && $_POST['phone_mob'])  || (!is_zipcode($_POST['zipcode']) && $_POST['zipcode']))
            {

               $this->show_warning('format_error');
                return;
            }
          
            $data = array(
                'user_id'       => $this->visitor->get('user_id'),
                'consignee'     => html_script(trim($_POST['consignee'])),
                'region_id'     => (int)$_POST['region_id'],
                'region_name'   => html_script(trim($_POST['region_name'])),
                'address'       => html_script(trim($_POST['address'])),
                'zipcode'       => html_script(trim($_POST['zipcode'])),
                'phone_mob'     => html_script(trim($_POST['phone_mob'])),
                'is_deafult'     => isset($_POST['default']) ? (int)$_POST['default'] : 0 ,
                'type'     => isset($_POST['type']) ? (int)$_POST['type'] : 0 ,
                
            );
            if($_POST['phone_code'] && $_POST['phone_section'])
            {
                if($_POST['phone_ext'])
                {
                     $data['phone_tel'] = trim($_POST['phone_section']).'-'.trim($_POST['phone_code']).'-'.trim($_POST['phone_ext']);
                }
                else
                {
                    $data['phone_tel'] = trim($_POST['phone_section']).'-'.trim($_POST['phone_code']);
                }
                if (!is_tel($data['phone_tel']))
                {  
                    $this->show_warning('电话号码不正确');
                    return;
                }
              
            }
            else
            {
                $data['phone_tel'] = '';
            }
            $model_selleraddress =& m('selleraddress'); 
            $sql = "SELECT COUNT(*) FROM {$model_selleraddress->table} WHERE `user_id` = " . $this->visitor->get('user_id') . " AND `type` = " . $data['type'];     
            if($data['type']==1)
            {
                if((int)$model_selleraddress->getOne($sql)>=10)
                {
                  $this->show_warning('add_address_error');
                  return;  
                }
                               
               $jup_url = 'index.php?app=seller_address&act=returns'; 
            }
            else
            {
                if((int)$model_selleraddress->getOne($sql)>=5)
                {
                  $this->show_warning('add_address_error');
                  return;  
                }
                $jup_url = 'index.php?app=seller_address&act=consignee'; 
            }
            
            if (!($address_id = $model_selleraddress->add($data)))
            {
                $this->show_warning($model_selleraddress->get_error());

                return;
            }
            if($data['is_deafult'] == 1)
            {
                $this->_set_default_address($address_id,$data['type']);
                
            }
            
          $this->show_message('add_address_successed','back_list', $jup_url);
        }
    }
    
    /**
     *    编辑地址
     *
     */
    function edit()
    {
        $addr_id = empty($_GET['addr_id']) ? 0 : intval($_GET['addr_id']);
        if (!IS_POST || !$addr_id)
        {
            $this->show_message('no_valid_data','back_list', 'index.php?app=seller_address&act=returns');                       
        }
        else
        {
            if (!$_POST['consignee'] || !$_POST['region_name'] || !$_POST['address'] || (!$_POST['phone_mob'] && !$_POST['phone_code']))
            {

               $this->show_warning('数据不完整!请重新填写!');
                return;
            }

            if ((!is_mobile($_POST['phone_mob']) && $_POST['phone_mob'])  || (!is_zipcode($_POST['zipcode']) && $_POST['zipcode']))
            {

               $this->show_warning('format_error');
                return;
            }
            
            $data = array(
                'consignee'     => html_script(trim($_POST['consignee'])),
                'region_id'     => (int)$_POST['region_id'],
                'region_name'   => html_script(trim($_POST['region_name'])),
                'address'       => html_script(trim($_POST['address'])),
                'zipcode'       => html_script(trim($_POST['zipcode'])),
                'phone_mob'     => html_script(trim($_POST['phone_mob'])),
                'is_deafult'     => isset($_POST['default']) ? (int)$_POST['default'] : 0 ,
                'type'     => isset($_POST['type']) ? (int)$_POST['type'] : 0 ,

            );
            if($_POST['phone_code'] && $_POST['phone_section'])
            {
                if($_POST['phone_ext'])
                {
                     $data['phone_tel'] = trim($_POST['phone_section']).'-'.trim($_POST['phone_code']).'-'.trim($_POST['phone_ext']);
                }
                else
                {
                    $data['phone_tel'] = trim($_POST['phone_section']).'-'.trim($_POST['phone_code']);
                }
                if (!is_tel($data['phone_tel']))
                {  
                    $this->show_warning('电话号码不正确');
                    return;
                }
              
            }
            else
            {
                $data['phone_tel'] = '';
            }
            $model_selleraddress =& m('selleraddress');
            $model_selleraddress->edit("addr_id = {$addr_id} AND user_id=" . $this->visitor->get('user_id'), $data);
            if ($model_selleraddress->has_error())
            {
                $this->show_warning($model_selleraddress->get_error());

                return;
            }
            if($data['is_deafult'] == 1)
            {
                $this->_set_default_address($addr_id,$data['type']);
                
            }
            if($data['type']==1)
            {
               $jup_url = 'index.php?app=seller_address&act=returns'; 
            }
            else
            {
                $jup_url = 'index.php?app=seller_address&act=consignee'; 
            }
                                    
            $this->show_message('edit_address_successed','back_list', $jup_url);

        }
    }
    
    /**
     *    设为默认地址
     *
     */
    function setdefault()
    {
        $addr_id = empty($_GET['addr_id']) ? 0 : intval($_GET['addr_id']);
        $type = empty($_GET['type']) ? 0 : intval($_GET['type']);
        if (!$addr_id)
        {
            echo Lang::get("no_such_address");
            return;
        }
        
        $model_selleraddress =& m('selleraddress');
        $model_selleraddress->edit("addr_id = {$addr_id} AND user_id=" . $this->visitor->get('user_id'), array('is_deafult'=>1));
        if ($model_selleraddress->has_error())
        {
            $this->show_warning($model_selleraddress->get_error());

            return;
        }
        $this->_set_default_address($addr_id,$type);
        $this->show_message('set_default_successed');
        
    }
    
    
    /**
     *    删除地址
     *
     */
    function drop()
    {
        $addr_id = isset($_GET['addr_id']) ? (int)($_GET['addr_id']) : 0;
        $addr_type =  isset($_GET['type']) ? (int)$_GET['type'] : 0 ;
        if (!$addr_id)
        {
            $this->show_warning('no_such_address');

            return;
        }
        $ids = explode(',', $addr_id);//获取一个类似array(1, 2, 3)的数组
        $model_selleraddress =& m('selleraddress');
        $drop_count = $model_selleraddress->drop("user_id = " . $this->visitor->get('user_id') . " AND addr_id " . db_create_in($ids));
        if (!$drop_count)
        {
            /* 没有可删除的项 */
            $this->show_warning('no_such_address');

            return;
        }

        if ($model_selleraddress->has_error())    //出错了
        {
            $this->show_warning($model_selleraddress->get_error());

            return;
        }
        if($addr_type == 1)
            {
               $jup_url = 'index.php?app=seller_address&act=returns'; 
            }
            else
            {
                $jup_url = 'index.php?app=seller_address&act=consignee'; 
            }
                                    
            $this->show_message('drop_address_successed','back_list', $jup_url);
    }
    
    /**
     *    获取区域信息
     *
     */
    function _get_regions()
    {
        $model_region =& m('region');
        $regions = $model_region->get_list(0);
        if ($regions)
        {
            $tmp  = array();
            foreach ($regions as $key => $value)
            {
                $tmp[$key] = $value['region_name'];
            }
            $regions = $tmp;
        }
        $this->assign('regions', $regions);
    }
    
    /**
     *    三级菜单
     *
     */
    function _get_member_submenu()
    {
        $menus = array(
            array(
                'name'  => 'address_list',
                'url'   => 'index.php?app=seller_address',
            ),
/*            array(
                'name'  => 'add_address',
                'url'   => 'index.php?app=my_address&act=add',
            ),*/
        );
/*        if (ACT == 'edit')
        {
            $menus[] = array(
                'name' => 'edit_address',
                'url'  => '',
            );
        }*/
        return $menus;
    }
    
    /**
     *    设为默认地址
     * @param int $addr_id 地址ID
     * @param int $type    退货1发货0
     *
     */
    function _set_default_address($addr_id,$type=0)
    {
        if(!$addr_id)
        {
            return ;
        }
        $model_selleraddress =& m('selleraddress');
        $model_selleraddress->edit("type = {$type} AND addr_id != {$addr_id} AND is_deafult=1 AND user_id=" . $this->visitor->get('user_id'), array('is_deafult'=>0));
        if ($model_selleraddress->has_error())
            {
                $this->show_warning($model_selleraddress->get_error());

                return ;
            }
        
    }

}

?>